package com.erp.order.purchasing.web;

import com.alibaba.tuna.client.api.MessageProcessException;
import com.alibaba.tuna.client.websocket.TunaWebSocketClient;
import com.alibaba.tuna.client.websocket.WebSocketMessage;
import com.alibaba.tuna.client.websocket.WebSocketMessageHandler;
import com.alibaba.tuna.client.websocket.WebSocketMessageType;
import com.alibaba.tuna.fastjson.JSONObject;
import com.erp.order.purchasing.service.impl.InquiryListServiceImpl;

/**
 * @author ZhangXuHao
 * @date 2021/04/18  19:01
 */
public class WebSocketClient {

    /**
     * 启动WebSocket
     */
    public void startWebSocketClient() {
        // 开放平台1688环境
        String url = "ws://message.1688.com/websocket";
        // 您的 AppKey
        String appKey = "7653499";
        // 您的应用秘钥
        String secret = "ijBnxoBdHq";
        // 您客户端设置的接收线程池大小 默认为虚拟机内核数*40 用户可以自行修改
        int threadNum = 160;
        // 1. 创建 Client，如果不传入threadNum参数的话，client将使用默认线程数
        TunaWebSocketClient client = new TunaWebSocketClient(appKey, secret, url);
        // 2. 创建 消息处理 Handler
        WebSocketMessageHandler tunaMessageHandler = new WebSocketMessageHandler() {
            /**
             * 消费消息。
             * 如果抛异常或返回 false，表明消费失败，如未达重试次数上限，开放平台将会择机重发消息
             */
            @Override
            public boolean onMessage(WebSocketMessage message) throws MessageProcessException {
                boolean success = true;
                /*
                 * 服务端推送的消息分为2种，
                 * 业务数据：SERVER_PUSH
                 * 系统消息：SYSTEM，如 appKey 与 secret 不匹配等，一般可忽略
                 */
                System.out.println("消息推送接口，返回数据:"+message);
                if(WebSocketMessageType.SERVER_PUSH.name().equals(message.getType())) {
                    try {
                        // json串
                        JSONObject data = JSONObject.parseObject(JSONObject.parseObject(message.getContent()).getString("data"));
                        switch (data.getString("status")) {
                            case "pending" :
                                new InquiryListServiceImpl().getPriceParity(data.getString("purchaseId"), data.getString("id"));
                                break;
                            case "" :
                                break;
                            default:
                        }
                    } catch (Exception e) {
                        success = false;
                    }
                }
                return success;
            }
        };
        client.setTunaMessageHandler(tunaMessageHandler);
        // 3. 启动 Client
        client.connect();
        // 4. 在应用关闭时，关闭客户端
        // client.shutdown();
    }

}